package com.wanmao.test.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.wanmao.test.bean.SBBean;
import com.wanmao.test.dbhelper.DBOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class SBService {
    private DBOpenHelper dBOpenHelper;

    public SBService(Context context) {
        this.dBOpenHelper = new DBOpenHelper(context);
    }

    public void save(SBBean bean) {
        SQLiteDatabase db = dBOpenHelper.getWritableDatabase();
        db.insert("SB_TABLE", null, settingValue(bean));
    }

    private ContentValues settingValue(SBBean bean) {
        ContentValues values = new ContentValues();
        values.put("SB_ID", bean.getSB_ID());
        values.put("GCID", bean.getGCID());
        values.put("SBMC", bean.getSBMC());
        values.put("SBLX", bean.getSBLX());
        values.put("DYDJ", bean.getDYDJ());
        values.put("SCCJ", bean.getSCCJ());
        values.put("GCMC", bean.getGCMC());
        return values;
    }

    public List<SBBean> findGCID(String GCid) {
        String sql = "select * from SB_TABLE where GCID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{GCid});

        return toListBean(cursor);

    }

    private List<SBBean> toListBean(Cursor cursor) {
        List<SBBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            SBBean bean = new SBBean();
            bean.setSB_ID(cursor.getString(cursor.getColumnIndex("SB_ID")));
            bean.setGCID(cursor.getString(cursor.getColumnIndex("GCID")));
            bean.setSBMC(cursor.getString(cursor.getColumnIndex("SBMC")));
            bean.setSBLX(cursor.getString(cursor.getColumnIndex("SBLX")));
            bean.setDYDJ(cursor.getString(cursor.getColumnIndex("DYDJ")));
            bean.setSCCJ(cursor.getString(cursor.getColumnIndex("SCCJ")));
            bean.setGCMC(cursor.getString(cursor.getColumnIndex("GCMC")));
            list.add(bean);
        }
        return list;
    }

    public List<SBBean> findSBLX(String SBLX) {
        String sql = "select * from SB_TABLE where SBLX=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{SBLX});
        return toListBean(cursor);

    }

    public List<SBBean> findSBLXbyGCID(String SBLX, String GCID) {
        String sql = "select * from SB_TABLE where SBLX=? and GCID=?";
        SQLiteDatabase db = dBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql, new String[]{SBLX, GCID});
        return toListBean(cursor);
    }
}
